package moplayer;

import java.util.Date;
import java.util.HashMap;
import java.util.Set;

import com.illposed.osc.OSCListener;
import com.illposed.osc.OSCMessage;

public class DeviceValueOSCFiller implements OSCListener {

	public static float x;
	public static float y;

	public static float wiiX;
	public static float wiiY;

	public static float nunchukJoystickX;
	public static float nunchukJoystickY;

	public static float nunchukX;
	public static float nunchukY;

	public static String command;

	public static HashMap<String, Boolean> buttonMap = new HashMap<String, Boolean>();
	public static HashMap<String, Boolean> toggleMap = new HashMap<String, Boolean>();

	public static boolean started;

	@Override
	public void acceptMessage(Date arg0, OSCMessage message) {
		if (!started) {
			populateMap(buttonMap);
			populateMap(toggleMap);
			started = true;
		}
		String address = message.getAddress();
		command = address;
		Object[] args = message.getArguments();
		float _x = (Float) args[0];
		float _y = (Float) args[1];
		if (args.length > 0) {
			if (address.equals("/wiimote")) {
				wiiX = _x;
				wiiY = _y;
			} else if (address.equals("/nunchuk")) {
				nunchukX = _x;
				nunchukY = _y;
			} else if (address.equals("/nunchukJoystick")) {
				nunchukJoystickX = _x;
				nunchukJoystickY = _y;
			} else {
				x = _x;
				y = _y;
			}
		}

		Set<String> setButtonNames = buttonMap.keySet();
		for (String buttonName : setButtonNames) {
			buttonPressing(address, buttonName);
			togglePressing(address, buttonName);
		}

		if (address.contains("button")) {
			System.out.println(address);
			// System.out.println("CMD=" + DeviceValueOSCFiller.command);
			// System.out.println("FRT=" +
			// DeviceValueOSCFiller.command.equals("/buttonHomePressed"));
			// System.out.println("SND=" +
			// DeviceValueOSCFiller.pressed("/buttonHome"));
		}

	}

	private static void populateMap(HashMap<String, Boolean> hashMap) {
		hashMap.put("/buttonA", false);
		hashMap.put("/buttonB", false);
		hashMap.put("/buttonPlus", false);
		hashMap.put("/buttonMinus", false);
		hashMap.put("/button1", false);
		hashMap.put("/button2", false);
		hashMap.put("/buttonHome", false);
		hashMap.put("/buttonLeft", false);
		hashMap.put("/buttonRight", false);
		hashMap.put("/buttonUp", false);
		hashMap.put("/buttonDown", false);
		hashMap.put("/buttonC", false);
		hashMap.put("/buttonZ", false);
	}

	public DeviceValueOSCFiller() {
		populateMap(buttonMap);
		populateMap(toggleMap);
		command = "";
	}

	private void buttonPressing(String address, String button) {
		String anObject = button + "Pressed";
		if (address.equals(anObject)) {
			buttonMap.put(button, true);
		} else if (address.equals(button + "Released")) {
			buttonMap.put(button, false);
		}
	}

	private void togglePressing(String address, String button) {
		String anObject = button + "Pressed";
		boolean currentValue = toggleMap.get(button);
		if (address.equals(anObject)) {
			toggleMap.put(button, !currentValue);
		}
	}

	public static boolean pressed(String button) {
		return DeviceValueOSCFiller.command.equals(button + "Pressed");
	}

	public static boolean released(String button) {
		return DeviceValueOSCFiller.command.equals(button + "Released");
	}

}
